const app = getApp();
const _request = require("../../utils/request.js");
require;
Page({
  data: {
    name: '',
    idcode: '',
    num: '',
    agree:true,
    upload1:[],
    upload2:[],
    upload3:[],
    upload4:[],
  },
  onLoad: function (){
    this.getStore()//回显
  },
  getStore(){
    _request.get('/storeAuthApply/getStoreAuthApplyByStoreId', { storeId:wx.getStorageSync('storeId') }).then((res) => {
      console.log(res)
      if (res.data.code == 200) {
        this.setData({
          name:res.data.data.realname,
          idcode:res.data.data.cardno,
          num:res.data.data.licenseno,
          upload1:res.data.data.businesslicensePath?[res.data.data.businesslicensePath]:[],
          upload2:res.data.data.storeBackgroundPath?[res.data.data.storeBackgroundPath]:[],
          upload3:res.data.data.storephotosPaths?res.data.data.storephotosPaths:[],
          upload4:res.data.data.handIDcardPath?[res.data.data.handIDcardPath]:[],
        })
      }
    })
  },
  getname(e){
    this.setData({ name:e.detail.value })
  },
  getidcode(e){
    this.setData({ idcode:e.detail.value })
  },
  getnum(e){
    this.setData({ num:e.detail.value })
  },
  goxieyi(){
    wx.navigateTo({
      url: '../../pages/auth/base/xieyi',
    })
  },
  gouploadimg(){
    console.log(this.data.upload3)
    wx.navigateTo({
      url: '../auth/upload_img?img='+ JSON.stringify(this.data.upload3),
    })
  },
  getagree(){
    this.setData({
      agree:!this.data.agree
    })
  },
  getdescribe(e){
    var index = e.currentTarget.dataset.index
    var text = ''
    if(index==1){
      text = '需要上传营业执照照片，拍摄区域能清晰可见所有人信息及执照编号。建议您参考示例，完成上传。图片符合要求才能更快完成审核！'
    }
    if(index==2){
      text = '需要上传门店封面照片，需包含完整牌匾，照片门头、门脸清晰可见。建议您参考示例，完成上传。图片符合要求才能更快完成审核！'
    }
    if(index==3){
      text = '需上传门店环境照可以真实反映店内情况，至少1张最多8张。建议您每张照片参考示例，完成上传！图片符合要求才能更快完成审核。'
    }
    if(index==4){
      text = '需要上传法人手持身份证照片，需包含人体上半身及身份证件，人脸、身份证件清晰可见。建议您参考示例，完成上传。图片符合要求才能更快完成审核！'
    }
    wx.showModal({
      title: '提示',
      content: text,
      confirmText:'我知道了',
      showCancel:false,
    })
  },
  // 上传
  afterRead(e) {
    let that = this
    wx.chooseImage({
      count:1,
      sourceType:['album', 'camera'],
      success(res) {
        if(e.currentTarget.dataset.index==1){
          that.setData({ upload1:res.tempFilePaths })
        }
        if(e.currentTarget.dataset.index==2){
          that.setData({ upload2:res.tempFilePaths })
        }
        if(e.currentTarget.dataset.index==3){
          that.setData({ upload3:res.tempFilePaths })
        }
        if(e.currentTarget.dataset.index==4){
          that.setData({ upload4:res.tempFilePaths })
        }
        var tempFilePaths = res.tempFilePaths[0]
        console.log(that.data.upload1)
        var self = that
        wx.uploadFile({
          url: app.globalData.baseurl + '/store-api/file/batchUpload',
          filePath: tempFilePaths,
          name: 'files',
          header: { 'token': wx.getStorageSync('token') },
          formData: { 'files': 'file' },
          success(res) {
            var data = JSON.parse(res.data)
            if(data.code==200){
              if(e.currentTarget.dataset.index==1){
                self.setData({ upload1:data.data })
              }
              if(e.currentTarget.dataset.index==2){
                self.setData({ upload2:data.data })
              }
              if(e.currentTarget.dataset.index==3){
                self.setData({ upload3:data.data })
              }
              if(e.currentTarget.dataset.index==4){
                self.setData({ upload4:data.data })
              }
            }else{
              wx.showToast({ title: '上传失败',icon:'none' })
              if(e.currentTarget.dataset.index==1){
                self.setData({ upload1:'' })
              }
              if(e.currentTarget.dataset.index==2){
                self.setData({ upload2:'' })
              }
              if(e.currentTarget.dataset.index==3){
                self.setData({ upload3:'' })
              }
              if(e.currentTarget.dataset.index==4){
                self.setData({ upload4:'' })
              }
            }
          },
          fail(error) {
            wx.showToast({ title: '上传失败',icon:'none' })
          },
        })
      }
    })
  },
  // 提交
  onNext() {
    if (!this.data.name) {
      wx.showToast({ title: "请输入真实姓名",icon: 'none' })
      return;
    }
    if (!this.data.idcode) {
      wx.showToast({ title: "请输入证件号码",icon: 'none' })
      return;
    }
    if (this.data.idcode.length<18) {
      wx.showToast({ title: "请输入正确的证件号码",icon: 'none' })
      return;
    }
    if (!this.data.num) {
      wx.showToast({ title: "请输入证件编号",icon: 'none' })
      return;
    }
    if (this.data.upload1.length<=0) {
      wx.showToast({ title: "请上传营业执照",icon: 'none' })
      return;
    }
    if (this.data.upload2.length<=0) {
      wx.showToast({ title: "请上传门店封面",icon: 'none' })
      return;
    }
    if (this.data.upload3.length<=0) {
      wx.showToast({ title: "请上传门店照片",icon: 'none' })
      return;
    }
    if (this.data.upload4.length<=0) {
      wx.showToast({ title: "请上传手持身份证照",icon: 'none' })
      return;
    }
    if (!this.data.agree) {
      wx.showToast({ title: "请阅读并同意协议",icon: 'none' })
      return;
    }
    _request.postObj('/storeAuthApply/save', {
      realname:this.data.name,
      cardno:this.data.idcode,
      licenseno:this.data.num,
      businesslicensePath:this.data.upload1[0],
      bannerPath:this.data.upload2[0],
      storephotosPaths:this.data.upload3,
      handIDcardPath:this.data.upload4[0],
      storeId:wx.getStorageSync('storeId'),
    }).then((res) => {
      if (res.data.code == 200) {
        wx.showToast({
          title: "上传成功",
          icon: 'none',
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/firstpages/home/index',
          })
        }, 2000);
      }
    })
  },
})